// VectorFields1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "VectorFields1.h"
#include "VectorFields1Dlg.h"
#include ".\vectorfields1dlg.h"

#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include "glMatrixScope.h"


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CVectorFields1Dlg dialog



CVectorFields1Dlg::CVectorFields1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVectorFields1Dlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVectorFields1Dlg::DoDataExchange(CDataExchange* pDX)
{

	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_DRAW_WND, m_drawWidget);
	DDX_Control(pDX, IDC_SLIDER_TRANSFER_SCALE, m_transferWidthSlider);
	DDX_Control(pDX, IDC_SLIDER_TRANSFER_OFF, m_transferOffSlider);
}

void CVectorFields1Dlg::OnMouseMove(UINT nFlags, CPoint point)
{
	// TRACE("HERE\n");
	CWnd::OnMouseMove(nFlags, point);
}

BEGIN_MESSAGE_MAP(CVectorFields1Dlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
	ON_WM_MOUSEMOVE()
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_TRANSFER_OFF, OnNMReleasedcaptureSliderTransferOff)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_TRANSFER_SCALE, OnNMReleasedcaptureSliderTransferScale)
END_MESSAGE_MAP()




// CVectorFields1Dlg message handlers

BOOL CVectorFields1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	char * szSrcData = "birdman_float_brick.dat";  //"dumb_ballistics_dump.dat";

	RawVolGrid tmpVolGrid;

	tmpVolGrid.ReadFromFile(szSrcData, 512, 512, 54, 4);  //512, 512, 50, 1);

	m_volGrid.ConvertFromRawVolGrid(tmpVolGrid, false);

	m_volViz.SetViewInfo(m_viewRotate);

	m_volViz.SetVolGrid(m_volGrid);

	m_volViz.SetStepSize(0, 4);
	m_volViz.SetStepSize(1, 4);
	m_volViz.SetStepSize(2, 2);

	m_viewRotate.SetViewBack(750.0);

	m_drawWidget.SetPerspective(true);
	m_drawWidget.SetCustomOnMouse(&m_viewRotate);

	m_drawWidget.AddCustomDraw(&m_viewRotate);
	m_drawWidget.AddCustomDraw(&m_volViz);

	CString strStats;
	strStats.Format("avg == %lf, stddev == %lf, min is %lf, max is %lf, numvalidsamps = %d", 
		m_volGrid.GetMean(), m_volGrid.GetStdDev(), 
		m_volGrid.GetMin(), m_volGrid.GetMax(),
		m_volGrid.GetNumValidSamples());
	
	AfxMessageBox(strStats);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVectorFields1Dlg::SetSlidersFromVolViz()
{
	double lfTransferOff = -m_volViz.GetTransferOffset() + 5.0;
	double lfTransferWidth = m_volViz.GetTransferWidth();

	m_transferOffSlider.SetPos(static_cast<int>(10*lfTransferOff));
	m_transferWidthSlider.SetPos(static_cast<int>(10*lfTransferWidth));
}

void CVectorFields1Dlg::SetVolVizFromSliders()
{
	double lfTransferOff = 0.1*m_transferOffSlider.GetPos() - 5.0;
	double lfTransferWidth = 0.1*m_transferWidthSlider.GetPos();

	//CString strMsg;
	//strMsg.Format("transferoff is %lf, transferwidth is %lf", lfTransferOff, lfTransferWidth);

	//AfxMessageBox(strMsg);

	m_volViz.SetTransferOffset(-lfTransferOff);
	m_volViz.SetTransferWidth(lfTransferWidth);
}

void CVectorFields1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVectorFields1Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		SetSlidersFromVolViz();
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVectorFields1Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CVectorFields1Dlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CVectorFields1Dlg::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

void CVectorFields1Dlg::OnNMReleasedcaptureSliderTransferOff(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: Add your control notification handler code here
	SetVolVizFromSliders();
	m_drawWidget.DrawScreen();
	*pResult = 0;
}

void CVectorFields1Dlg::OnNMReleasedcaptureSliderTransferScale(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: Add your control notification handler code here
	SetVolVizFromSliders();
	m_drawWidget.DrawScreen();
	*pResult = 0;
}
